package com.example.week12_networkprogramming;

import android.icu.text.SimpleDateFormat;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class MysqlConnectionActivity extends AppCompatActivity {

    private EditText readerNumEdt,readerNameEdt,readerPhoneEdt;
    private TextView resultTxt;
    private RadioGroup readerTypeRadioGroup;
    Reader reader=null;

    public Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case 0x11:
                    String s=(String) msg.obj;
                    resultTxt.setText(s);
                    break;
                case 0x12:
                    String ss=(String)msg.obj;
                    resultTxt.setText(ss);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mysql_connection);

        readerNameEdt=(EditText) findViewById(R.id.readerNameEdt);
        readerNumEdt=(EditText) findViewById(R.id.readerNumEdt);
        readerPhoneEdt=(EditText) findViewById(R.id.readerPhoneEdt);
        resultTxt=(TextView) findViewById(R.id.resultText);
        readerTypeRadioGroup=(RadioGroup) findViewById(R.id.readerTypeRadioGroup);

        findViewById(R.id.queryReaderBnt).setOnClickListener(View.OnClickListener);
    }

    private View.OnClickListener OnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Message message=handler.obtainMessage();
            message.what=0x11;

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"+"HH:mm:ss");

            switch (v.getId())
            {
                case R.id.addReaderBnt:
                    break;
                case R.id.deleteReaderBnt:
                 /*  int delCount=DBUtils.delete_reader(readerNameEdt.getText().toString());
                    message.obj="已经删除"+delCount+"名读者数据。";
                    break;*/
                case R.id.updateReaderBnt:
                /*reader=new Reader(readerNumEdt.getText().toString(),readerNameEdt.getText().toString(),getReaderType(),readerPhoneEdt.getText().toString(),null,sdf.format(new Date()).toString());
                int updateCount=0;
                updateCount=DBUtils.update_reader(reader);
                message.obj="已经修改"+updateCount+"名读者数据。";
                break;*/

                case R.id.queryReaderBnt:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            ResultSet res=DBUtils.queryReader(readerNumEdt.getText().toString());
                            String str="";
                            int i=1;
                            try
                            {
                                while(res.next())
                                {
                                    Reader reader=new Reader();
                       reader.setReader_number(res.getString("reader_number"));
                      reader.setReader_name(res.getString("reader_name"));
                      reader.setReader_type(res.getString("reader_type"));
                      reader.setReader_phone(res.getString("reader_phone"));

                                }
                            }catch (SQLException throwables)
                            {
                                throwables.printStackTrace();
                            }
                            if(str!="")
                            {
                                message.what=0x12;
                                message.obj="查询结果:\n"+str;
                            }
                            else
                            {
                                message.obj="查询结果为空";
                            }
                            handler.sendMessage(message);
                        }
                    }).start();
                      break;
            }
        }
    };
}
